﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.ExceptionHandler;

public partial class Stock_SubRubrosAdmin : GenericPage
{
    List<Rubros> listaRubros = new List<Rubros>();    
    Rubros oRubro = new Rubros();
    List<SubRubros> listaSubRubros = new List<SubRubros>();    
    SubRubros oSubRubro = new SubRubros();

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                tblGestion.Visible = false;
                hdnAccion.Value = "";
                txtCodigo.Text = "";
                txtSubRubro.Text = "";
                txtSubRubro2.Text = "";
                CargarRubros();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region cboRubro_SelectedIndexChanged
    protected void cboRubro_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            listaSubRubros = new List<SubRubros>();
            Session["listaSubRubros"] = listaSubRubros;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnBuscar_Click
    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            BMSubRubros bmSubRubros = new BMSubRubros(Connection);
            oSubRubro.Id_Rubro = Convert.ToInt16(cboRubro.SelectedValue);
            if (txtCodigo.Text.Trim() != "")
            {
                oSubRubro.Id_SubRubro = Convert.ToInt16(txtCodigo.Text.Trim());
            }
            else
            {
                oSubRubro.Id_SubRubro = 0;
            }

            oSubRubro.SubRubro = txtSubRubro.Text.Trim();

            listaSubRubros = bmSubRubros.BMSubRubrosSearch(oSubRubro);

            Session["listaSubRubros"] = listaSubRubros;
            CargarGrilla();

        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnCancelar_Click
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Seguridad/Default.aspx", false);
    }
    #endregion

    #region gvSubRubros_PageIndexChanging
    protected void gvSubRubros_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvSubRubros.PageIndex = e.NewPageIndex;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region gvSubRubros_SelectedIndexChange
    protected void gvSubRubros_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            oSubRubro = new SubRubros();
            oSubRubro.Id_Rubro = Convert.ToInt16(cboRubro.SelectedValue);
            oSubRubro.Id_SubRubro = (int)gvSubRubros.SelectedDataKey.Value;
            listaSubRubros = (List<SubRubros>)Session["listaSubRubros"];
            foreach (SubRubros item in listaSubRubros)
            {
                if (item.Id_SubRubro == oSubRubro.Id_SubRubro)
                {
                    oSubRubro.Id_Rubro = item.Id_Rubro;
                    oSubRubro.Id_SubRubro = item.Id_SubRubro;
                    txtSubRubro2.Text = item.SubRubro;
                    break;
                }
            }
            Session["SubRubro"] = oSubRubro;
            tblAdmin.Visible = false;
            tblGestion.Visible = true;
            lblAccion.Text = "Modificar Descripción de SubRubro";
            hdnAccion.Value = "Modifica";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnNuevo_Click
    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        try
        {
            tblAdmin.Visible = false;
            tblGestion.Visible = true;
            lblAccion.Text = "Ingresar Descripción de Nuevo SubRubro";
            hdnAccion.Value = "Alta";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnGuardar_Click
    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtSubRubro2.Text != "")
            {
                if (hdnAccion.Value == "Alta")
                {
                    AltaSubRubro();
                }
                if (hdnAccion.Value == "Modifica")
                {
                    ModificaSubRubro();
                }
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #endregion

    #region Metodos

    #region CargarRubros
    protected void CargarRubros()
    {
        try
        {
            BMRubros bmRubros = new BMRubros(Connection);
            oRubro = new Rubros();
            listaRubros = bmRubros.BMRubrosSearch(oRubro);
            foreach (Rubros itemR in listaRubros)
            {
                ListItem item = new ListItem();
                item.Value = itemR.Id_Rubro.ToString();
                item.Text = itemR.Rubro;
                cboRubro.Items.Add(item);
            }
            Session["listaRubros"] = listaRubros;
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region CargarSubRubros
    protected void CargarSubRubros()
    {
        try
        {
            BMSubRubros bmSubRubros = new BMSubRubros(Connection);
            oSubRubro.Id_Rubro = Convert.ToInt16(cboRubro.SelectedValue);
            listaSubRubros = bmSubRubros.BMSubRubrosSearch(oSubRubro);
            Session["listaSubRubros"] = listaSubRubros;
            CargarGrilla();
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region CargarGrilla
    protected void CargarGrilla()
    {
        try
        {
            listaSubRubros = (List<SubRubros>)Session["listaSubRubros"];
            gvSubRubros.DataSource = listaSubRubros;
            gvSubRubros.DataBind();
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region ListaSubRubros
    protected void ListaSubRubros()
    {
        try
        {
            BMSubRubros bmSubRubros = new BMSubRubros(Connection);
            oSubRubro = new SubRubros();
            oSubRubro.Id_Rubro = Convert.ToInt16(cboRubro.SelectedValue);
            listaSubRubros = new List<SubRubros>();
            listaSubRubros = bmSubRubros.BMSubRubrosSearch(oSubRubro);
            Session["listaSubRubros"] = listaSubRubros;
            CargarGrilla();
            tblGestion.Visible = false;
            hdnAccion.Value = "";
            txtCodigo.Text = "";
            txtSubRubro.Text = "";
            txtSubRubro2.Text = "";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region AltaSubRubro
    protected void AltaSubRubro()
    {
        try
        {
            BMSubRubros bmSubRubros = new BMSubRubros(Connection);
            oSubRubro = new SubRubros();
            oSubRubro.Id_Rubro = Convert.ToInt16(cboRubro.SelectedValue);
            oSubRubro.SubRubro = txtSubRubro2.Text;
            bmSubRubros.BMSubRubrosInsert(oSubRubro);
            tblAdmin.Visible = true;
            tblGestion.Visible = false;
            ListaSubRubros();
            lblAccion.Text = "SubRubros Actualizados";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region ModificaSubRubro
    protected void ModificaSubRubro()
    {
        try
        {
            BMSubRubros bmSubRubros = new BMSubRubros(Connection);
            oSubRubro = new SubRubros();
            oSubRubro = (SubRubros)Session["SubRubro"];
            oSubRubro.SubRubro = txtSubRubro2.Text;
            bmSubRubros.BMSubRubrosUpdate(oSubRubro);
            tblAdmin.Visible = true;
            tblGestion.Visible = false;
            ListaSubRubros();
            lblAccion.Text = "SubRubros Actualizados";
            gvSubRubros.SelectedIndex = -1;
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #endregion
  
}